package com.atguigu.system.service.impl;

import com.atguigu.model.system.SysCalendar;
import com.atguigu.system.mapper.SysCalendarMapper;
import com.atguigu.system.service.SysCalendarService;
import com.atguigu.system.utils.HolidayChecker;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 日历表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-06-19
 */
@Service
public class SysCalendarServiceImpl extends ServiceImpl<SysCalendarMapper, SysCalendar> implements SysCalendarService {

    @Resource
    private SysCalendarMapper sysCalendarMapper;

    @Override
    public void batchDateByMonth(Map<String, Integer> param) {
        Integer month = getMonth(param);
        List<SysCalendar> list = findList(param);
        if (list.isEmpty()){
            List<SysCalendar> dataList = HolidayChecker.getDataList(month);
            sysCalendarMapper.batchInsert(dataList);
        }
    }

    private Integer getMonth(Map<String,Integer> param){
        Integer month = param.get("month");
        if (Objects.isNull(param.get("month"))){
            month = Calendar.getInstance().get(Calendar.MONTH) + 1;
        }
        return month;
    }

    @Override
    public List<SysCalendar> findList(Map<String,Integer> param) {
        Integer month = getMonth(param);
        QueryWrapper<SysCalendar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sys_month", month);
        List<SysCalendar> list = sysCalendarMapper.selectList(queryWrapper);
        return list;
    }
}
